home *** CD-ROM | disk | FTP | other *** search
- #include <Events.h>
- #include <memory.h>
- #include <types.h>
- #include <OSUtils.h> /* for SysBeep */
-
- #include <stdio.h>
-
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/errno.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <netinet/in.h>
- #include <sys/uio.h>
-
- #include "tcpglue.h"
- #include "socket.internal.h"
-
- static struct timeval selectPoll = {0,0};
- main()
- {
- int status;
- int s;
- struct sockaddr_in me, her, name,from;
- int namelen;
- int herlen = sizeof(her);
- int fromlen = sizeof(from);
- int bytes;
- char line[1000];
- int count,readfds, writefds, exceptfds;
-
- #if 1
- dprintf("address %08x\n",xIPAddr());
- dprintf("netmask %08x\n",xNetMask());
- dprintf("max mtu %d\n",xMaxMTU());
- #endif
-
- /* make our socket */
- s = s_socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0)
- {
- perror("socket");
- exit(1);
- }
-
- /* bind it to port zero - ie. let MacTCP pick a port */
- bzero((char *)&me, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_port = htons(25);
- if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
- {
- perror("bind");
- exit(1);
- }
-
- #if 1
- /* go non-blocking */
- if (s_ioctl(s,FIONBIO,0) < 0)
- {
- perror("ioctl(FIONBIO)");
- exit(1);
- }
- #endif
-
- /* set the address of the server */
- her.sin_family = AF_INET;
- her.sin_addr.s_addr = /* 0x0d000ce8 */ 0x8064660a;
- her.sin_port = htons(69); /* TFTP */
-
- /* send a request - a TFTP RRQ (read request) */
- udpCheckNotify();
- BlockMove("\000\001rubbish\000netascii\000",line,19);
- if (s_sendto(s,line,19/*bytes*/,0/*flags*/,&her,herlen) <= 0 && errno != EINPROGRESS)
- {
- perror("send");
- exit(1);
- }
-
- /* wait for it to get there */
- for (;;)
- {
- udpCheckNotify();
- writefds = 0xffffffff;
- count = s_select(32, NULL, &writefds, NULL, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- for (;;)
- {
- /* wait for the response */
- for (;;)
- {
- udpCheckNotify();
- readfds = 0xffffffff;
- count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* retreive the response */
- udpCheckNotify();
- bytes = s_recvfrom(s,line,sizeof(line)-2,0/*flags*/,&from,&fromlen);
- if (bytes < 0)
- {
- perror("read");
- exit(1);
- }
- dprintf("recv got %d bytes from %08x/%d\n",
- bytes,from.sin_addr.s_addr,from.sin_port);
-
- #ifdef 0
- hex_dump(line,16,"");
- #endif
-
- /* note who answered us - probably a different port */
- /* only needs to be done the first time around */
- BlockMove((Ptr)&from,(Ptr)&her,herlen);
-
- if (line[1] != 3/*data block*/)
- break;
-
- /* wow! the file exists and we can read it - send an ack */
- udpCheckNotify();
- line[1] = 4/*ack*/;
- if (s_sendto(s,line,4/*bytes*/,0/*flags*/,&her,herlen) <= 0 && errno != EINPROGRESS)
- {
- perror("send");
- exit(1);
- }
-
- /* wait for it to get there */
- for (;;)
- {
- udpCheckNotify();
- writefds = 0xffffffff;
- count = s_select(32, NULL, &writefds, NULL, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- if (bytes < 516)
- break;
- }
-
- /* clean up */
- udpCheckNotify();
- if (s_close(s) < 0)
- {
- perror("close(s)");
- exit(1);
- }
-
- /* spin in case something odd happens at the end */
- for (;;)
- {
- udpCheckNotify();
- }
- }
-
-